//
//  ChoiceBtnView.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/5/20.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "ChoiceBtnView.h"
#import "NSString+Mantis.h"



static NSInteger btnTag=222;//标记按钮tag
@interface ChoiceBtnView ()
@property(nonatomic,strong)NSArray<MantisTextCellSubContentData*>*btnArr;
@end



@implementation ChoiceBtnView
-(instancetype)initWithFrame:(CGRect)frame{
    self =[super initWithFrame:frame];
    if (self) {
      
    }
    return self;
}
-(void)reloadChoceBtnViewWith:(NSArray*)btnArr{
    self.btnArr=btnArr;
       
        CGFloat btnBgWidth=IMSCREEN_WIDTH-adaptWidth750(100*2);
        
        CGFloat btnBgHeight=adaptWidth750(200);
       
        int rowNum=3;//每行排4个
       
        for (int i = 0; i < btnArr.count; i++)
          {
              UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
              button.backgroundColor =i==0? [UIColor redColor]:[UIColor blueColor];
              button.tag = i+btnTag;
              button.lineBreakMode=NSLineBreakByTruncatingTail;
              //确定button位置
              //3.在第几行
              int row =  i / rowNum;
              //4.在第几列
              int cloumn = i % rowNum;
              
              CGFloat w = btnBgWidth;
              CGFloat h = btnBgHeight;
              CGFloat blankWidth=12;
              CGFloat blankheight=5;
              CGFloat bw = (w-4*blankWidth)/3;
              CGFloat bh = (h-4*blankheight)/3;
              
              CGFloat x = blankWidth + (bw+blankWidth) * cloumn;
              CGFloat y = (bh+blankheight) * row+blankheight;
              
              button.frame = CGRectMake(x, y, bw, bh);
              NSLog(@"+++%@",NSStringFromCGRect(button.frame));
              [button addTarget:self action:@selector(tapFaceButton:) forControlEvents:UIControlEventTouchUpInside];
              
              [self addSubview:button];
              
                ViewRadius(button, 4);
              
              //刷新btn数据
               MantisTextCellSubContentData*btnData =btnArr[i];
              if ([NSString isBlankString:btnData.bgColor]) {
                  btnData.bgColor=@"#06CB94";
              }
                button.backgroundColor =[UIColor jk_colorWithHexString:btnData.bgColor];
              [button setTitle:btnData.label forState:UIControlStateNormal];
          }
}



-(void)tapFaceButton:(UIButton*)sender{
    NSInteger index=sender.tag-btnTag;
    MantisTextCellSubContentData*btnData=self.btnArr[index];
    !self.btnTitleBlock?:self.btnTitleBlock(btnData);
}












/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
